package com.swhysc.information.repository;


import com.swhysc.information.entity.InformationDataTableEx;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

public interface InformationDataTableExRepository extends JpaRepository<InformationDataTableEx, String>, QuerydslPredicateExecutor<InformationDataTableEx> {
    int deleteInformationDataTablesByIdIn(List<String> ids);

    /**
     * 查询dirId集合匹配的数据
     * @param id id
     * @return int个数
     */
    List<InformationDataTableEx> findByDirId(String id);


    List<InformationDataTableEx> findByDirIdOrderBySortAscUpdateTimeDescEnNameAsc(String id);

    /**
     * 查询DataSourceId集合匹配的数据
     * @param id id集合
     * @return int个数
     */
    List<InformationDataTableEx> findByDataSourceId(String id);

    @Transactional
    int deleteInformationDataTablesByEnNameInAndDataSourceId(List<String> enNames, String id);


    @Query(value = "SELECT d.NAME as dataSourceName,t.ID,t.EN_NAME,t.CH_NAME,t.FREQUENCY,t.CONTENT,t.DIR_ID,t.DATA_SOURCE_ID,dir.NAME as tempLevel,dir.LEVEL,dir.IS_LEAF,dir.PARENT_ID  " +
            "FROM INFORMATION_DATA_TABLE_EX t " +
            "INNER JOIN  INFORMATION_DATA_DIRECTORY_EX d " +
            "on t.DATA_SOURCE_ID = d.ID " +
            "INNER JOIN  INFORMATION_DATA_DIRECTORY_EX dir " +
            "on t.DIR_ID= dir.ID " +
            "where d.ID IN ?1 ", nativeQuery = true)
    List<Object[]> getTableAndDirByDirIds(Set<String> dirIds);
}

